<template>
  <el-dialog
      :title="title"
      v-if="showDom"
      v-el-drag-dialog
      :visible.sync="visible"
      width="720px"
      @closed="closed">
    <div class="">
      <div class="tag-group">
        <div class="tag-item-col"
             v-for="(item, index) in typeLists"
             :key="index">
          <div class="tag-item" @click="cliTag(item)" :title="item.className">
            <span>{{item.className}}</span>
            <!--<i class="el-icon-circle-plus-outline"></i>-->
          </div>
        </div>
      </div>
      <div class="selected-cont-group">
        <el-scrollbar style="height: 100%">
          <div class="selected-cont">
            <el-form size="mini" ref="queryForm" inline label-width="80px">
              <el-row v-for="(item,index) in selectedDatas" :key="index">
                <el-col :span="8">
                  <el-form-item label="参数类型">
                    <el-input
                        style="width: 130px"
                        v-model="item.name" readonly
                        placeholder="参数类型"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="参数名称">
                    <el-input
                        style="width: 130px"
                        v-model="item.value"
                        placeholder="参数名称"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="参数位置">
                    <el-select
                        style="width: 130px"
                        v-model="item.sort"
                        placeholder="参数位置" clearable>
                      <el-option :label="index" :value="index" v-for="(item,index) in 100" :key="index"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>

              </el-row>
            </el-form>
          </div>
        </el-scrollbar>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="hide">取 消</el-button>
      <el-button size="small" type="primary" @click="addType2">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {get_getPClass} from "@/apis/assembly/assembly";

export default {
  name: "StructureParamDialog",
  data() {
    return {
      funItem: null,
      funIndex: 0,
      paramIndex: 0,
      title: '',
      type: 1,
      showDom: false,
      visible: false,
      readonly: true,
      typeLists: [],
      selectedDatas: [],
      structureParam: null
    }
  },
  methods: {
    show({item, index}, paramIndex, param = {}) {
      this.visible = true;
      this.showDom = true;
      this.funItem = item
      this.funIndex = index
      this.paramIndex = paramIndex
      this.getTypes()

      this.title = `选择${param.className || param.name}参数`
      this.structureParam = param
      console.log(param)
      if (param.list && param.list.length) {
        this.selectedDatas = JSON.parse(JSON.stringify(param.list))
      } else {
        this.selectedDatas = []
      }
    },
    hide() {
      this.visible = false
    },
    closed() {
      this.showDom = false
    },

    getTypes(type) {
      get_getPClass().then((res) => {
        let datas = []
        res.data.data.forEach((item, index) => {
          if (item.classType !== '1') {
            datas.push(item)
          }
        })
        this.typeLists = datas
      });
    },
    //选择类型
    cliTag(item) {
      this.selectedDatas.push({
        name: item.className,
        value: '',
        sort: ''
      });
    },
    //参数类型删除
    delType(val, index) {
      this.selectedDatas.splice(index, 1);
    },
    addType() {
      this.hide()
      this.$emit('selected', this.selectedDatas, this.funIndex)
    },

    // 结构体
    addType2() {
      let params = this.selectedDatas.map(item => {
        return {
          fieldName: item.value,
          fieldOrder: item.sort,
          fieldType: item.name,
          // pclassId: this.structureParam.id,
        }
      })

      this.$emit('selected', params, this.paramIndex)
      this.hide()
    }
  }
}
</script>

<style scoped lang="scss">

  .tag-group {
    display: flex;
    flex-wrap: wrap;
    margin-left: -5px;
    margin-right: -5px;

    .tag-item-col {
      padding-left: 5px;
      padding-right: 5px;
    }

    .tag-item {
      display: block;
      width: 100px;
      text-align: center;
      border: 1px solid #216892;
      line-height: 30px;
      cursor: pointer;
      position: relative;
      border-radius: 4px;
      color: #ffffff;
      margin-bottom: 10px;
      transition: all 0.3s;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;

      &:hover {
        background-color: #216892;
      }

      .el-icon-circle-close {
        position: absolute;
        right: 5px;
        top: 5px;
        cursor: pointer;
      }
    }
  }

  .selected-cont-group {
    border: 1px solid #216892;
    width: 100%;
    height: 300px;
    margin-top: 10px;
    border-radius: 4px;
    padding: 5px;
  }

  .selected-cont {
    .selected-item {
      width: calc(33.3333% - 10px);
      text-align: center;
      border: 1px solid #216892;
      background-color: #ffffff;
      height: 30px;
      line-height: 30px;
      margin: 5px;
      cursor: pointer;
      position: relative;
      display: flex;
      align-items: center;
      padding-right: 20px;
      border-radius: 4px;
      overflow: hidden;

      .label-text {
        width: 80px;
        flex-shrink: 0;
        height: 100%;
        background-color: #ececec;
        border-right: solid 1px #216892;
      }

      .type-input {
        width: 100%;
        height: 100%;
        border: none;
        padding: 0 10px;
      }

      .el-icon-circle-close {
        position: absolute;
        width: 14px;
        height: 14px;
        right: 5px;
        top: 0;
        bottom: 0;
        margin: auto 0;
        cursor: pointer;
      }
    }
  }
</style>